package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.api.UserManagerClick;
import com.lenovo.smarttraffic.bean.LitepalBean.DBUserInfoBean;

import java.util.List;

/**
 * Created by Caleb.F
 * on 2020-03-13
 * at 21:52
 */
public class MyUserInfoRecyclerAdapter extends RecyclerView.Adapter<MyUserInfoRecyclerAdapter.MyViewHolder> {
    private List<DBUserInfoBean> mlist;
    private Context mContext;
    private UserManagerClick mUserManagerClick;

    public MyUserInfoRecyclerAdapter(List<DBUserInfoBean> list, Context context) {
        mlist = list;
        mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.userinfo_datail, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if (mlist.get(position).getPsex().equals("男")) {
            holder.mImageView.setImageResource(R.mipmap.touxiang_2);
        } else {
            holder.mImageView.setImageResource(R.mipmap.touxiang_1);
        }
        holder.mTvUsername.setText(new StringBuilder().append("用户名: ").append(mlist.get(position).getUsername()));
        holder.mTvPname.setText(new StringBuilder().append("姓名: ").append(mlist.get(position).getPname()));
        holder.mTvTelephone.setText(new StringBuilder().append("电话: ").append(mlist.get(position).getPtel()));
        holder.mTvCollect.setOnClickListener(v -> {
            mUserManagerClick.collectClick(v, holder.getAdapterPosition());
        });
        holder.mImageView.setOnClickListener(v -> {
            mUserManagerClick.imageClick(v);
        });
        holder.mTvDetail.setOnClickListener(v -> {
            mUserManagerClick.detailClick(v);
        });
    }

    public void setUserManagerCllick(UserManagerClick userManagerCllick) {
        mUserManagerClick = userManagerCllick;
    }

    public List<DBUserInfoBean> getMlist() {
        return mlist;
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView mImageView;
        TextView mTvUsername;
        TextView mTvPname;
        TextView mTvTelephone;
        TextView mTextView5;
        TextView mTvDetail;
        TextView mTvCollect;

        public MyViewHolder(View convertView) {
            super(convertView);
            mImageView = convertView.findViewById(R.id.imageView);
            mTvUsername = convertView.findViewById(R.id.tv_username);
            mTvPname = convertView.findViewById(R.id.tv_pname);
            mTvTelephone = convertView.findViewById(R.id.tv_telephone);
            mTextView5 = convertView.findViewById(R.id.textView5);
            mTvDetail = convertView.findViewById(R.id.tv_detail);
            mTvCollect = convertView.findViewById(R.id.tv_collect);
        }
    }
}
